#include "pch.h"

#include "MediaReader.h"
#include "AudioEngine.h"

using namespace Platform;
using namespace Microsoft::WRL;
using namespace Windows::Foundation;

AudioEngine::AudioEngine(void)
{
}

void AudioEngine::Initialize()
{
	m_audio = ref new Audio();
	m_audio->CreateDeviceIndependentResources();

	m_mediaReader = ref new MediaReader();

	// Impacts
	m_soundMap[SoundCue::BallLaunch]	= LoadSound("Sounds\\Impacts\\BallLaunch.wav");
	m_soundMap[SoundCue::Buzz]			= LoadSound("Sounds\\Impacts\\Buzz.wav");
	m_soundMap[SoundCue::Impact1]		= LoadSound("Sounds\\Impacts\\Impact1.wav");
	m_soundMap[SoundCue::Impact2]		= LoadSound("Sounds\\Impacts\\Impact2.wav");
	m_soundMap[SoundCue::Impact3]		= LoadSound("Sounds\\Impacts\\Impact3.wav");
	m_soundMap[SoundCue::PuckPaddle]	= LoadSound("Sounds\\Impacts\\PuckPaddle.wav");

	// Loops
	m_soundMap[SoundCue::Intro]			= LoadSound("Sounds\\Loops\\Intro.wav");
	m_soundMap[SoundCue::Gameplay1]		= LoadSound("Sounds\\Loops\\Gameplay1.wav");
	m_soundMap[SoundCue::Gameplay2]		= LoadSound("Sounds\\Loops\\Gameplay2.wav");
	m_soundMap[SoundCue::Gameplay3]		= LoadSound("Sounds\\Loops\\Gameplay3.wav");
	m_soundMap[SoundCue::WaitingRoom]	= LoadSound("Sounds\\Loops\\WaitingRoom.wav");

	// Voices
	m_soundMap[SoundCue::Three]			= LoadSound("Sounds\\Voices\\3.wav");
	m_soundMap[SoundCue::Two]			= LoadSound("Sounds\\Voices\\2.wav");
	m_soundMap[SoundCue::One]			= LoadSound("Sounds\\Voices\\1.wav");
	m_soundMap[SoundCue::Ignition]		= LoadSound("Sounds\\Voices\\Ignition.wav");
	m_soundMap[SoundCue::Defeat]		= LoadSound("Sounds\\Voices\\Defeat.wav");
	m_soundMap[SoundCue::Victory]		= LoadSound("Sounds\\Voices\\Victory.wav");
}

SoundEffect^ AudioEngine::LoadSound(String^ filename)
{
	Array<byte>^ soundData = m_mediaReader->LoadMedia(filename);
	auto soundEffect = ref new SoundEffect();
	soundEffect->Initialize(m_audio->SoundEffectEngine(), m_mediaReader->GetOutputWaveFormatEx(), soundData);
	return soundEffect;
}

void AudioEngine::PlaySound(SoundCue cue, float volume, bool loop)
{
	m_soundMap[cue]->Play(volume, loop);
}

void AudioEngine::SetVolume(SoundCue cue, float volume)
{
	m_soundMap[cue]->SetVolume(volume);
}


void AudioEngine::StopSound(SoundCue cue, bool waitUntilEnd)
{
	m_soundMap[cue]->Stop(waitUntilEnd);
}

void AudioEngine::StopAllSounds()
{
	std::map<SoundCue,SoundEffect^>::iterator pos;

	for(pos = m_soundMap.begin(); pos != m_soundMap.end(); ++pos)
	{
		SoundEffect^ se = dynamic_cast<SoundEffect^>(pos->second);
		se->Stop(false);
	}
}

bool AudioEngine::IsPlaying(SoundCue cue)
{
	return m_soundMap[cue]->IsPlaying();
}

void AudioEngine::PlayBounce(float volume)
{
	int r = rand() % 3 + 1;
	switch(r)
	{
		case 1:
			m_soundMap[SoundCue::Impact1]->Play(volume, false);
			break;
		case 2:
			m_soundMap[SoundCue::Impact2]->Play(volume, false);
			break;
		case 3:
			m_soundMap[SoundCue::Impact3]->Play(volume, false);
			break;
	}
}